package com.fbee.smartcommunities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fbee.smartcommunities.entity.ParkingSpace;
import com.fbee.smartcommunities.entity.Repair;
import com.fbee.smartcommunities.service.ParkingSpaceService;
import com.fbee.smartcommunities.vo.ParkingSpaceVO;
import com.fbee.smartcommunities.vo.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-21
 */
@RestController
@RequestMapping("/parkingSpace")
public class ParkingSpaceController {

    @Resource
    private ParkingSpaceService parkingSpaceService;

    // 分页后内容
    @GetMapping("/list")
    public ResultVO<IPage<ParkingSpace>> getPage(Integer pageNo, Integer pageSize, String keyword, String part) {
        return ResultVO.success(parkingSpaceService.getPage(pageNo,pageSize,keyword,part));
    }

    @PostMapping("/changeState")
    public ResultVO<Boolean> changeState(Long id,Integer state){
        return ResultVO.success(parkingSpaceService.update(new UpdateWrapper<ParkingSpace>().set("state",state).set("user_id",-1).eq("id",id)),"修改成功");
    }

    @GetMapping("/getTargetList")
    public ResultVO<List<ParkingSpace>> getTargetList(String targetSpace){
        return ResultVO.success(parkingSpaceService.list(new QueryWrapper<ParkingSpace>().eq("location",targetSpace)));
    }

    @PostMapping("/update")
    public ResultVO<Boolean> updateParkingSpace(Long id,Integer state,Long uid,Long begin,Long end){
        return ResultVO.success(parkingSpaceService.updateParkingSpace(id,state,uid,begin,end,-1d,null));
    }

    @PostMapping("/updateState")
    public ResultVO<Boolean> updateState(Long id, Integer state, Long openId, Long begin, Long end, Double price, HttpServletRequest request){
        return ResultVO.success(parkingSpaceService.updateParkingSpace(id, state, openId, begin, end,price,request),"操作成功");
    }

    @GetMapping("/getMySpace")
    public ResultVO<ParkingSpaceVO> getMyParkingSpace(Long openId){
        return ResultVO.success(this.parkingSpaceService.getMyParkingSpace(openId));
    }

}
